//
//  AreaCellNode.m
//  EducationLive
//
//  Created by Apple on 2017/10/19.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "AreaCellNode.h"

@interface AreaCellNode ()
// UI
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASImageNode *arrowNode;
@property (nonatomic, strong) ASImageNode *lineNode;
// Data
@property (nonatomic, copy) NSString *title;
// 设置地区二级页面
@property (nonatomic, assign) BOOL areaSecondLevel;

@end

@implementation AreaCellNode

- (instancetype)initWithItem:(NSString *)title areaSecondLevel:(BOOL)areaSecondLevel
{
    if (self = [super init]) {
        
        _areaSecondLevel = areaSecondLevel;
        
        _title = title;
        
        self.backgroundColor = [UIColor whiteColor];
        
        if (!_areaSecondLevel) {
            self.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        [self addTitleNode];
        
        [self addArrowNode];
        
        [self addLineNode];
    }
    return self;
}

- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.placeholderEnabled = YES;
    titleNode.placeholderColor = RGB_255(245, 245, 245);
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0f] ,NSForegroundColorAttributeName: RGB_255(51, 51, 51)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:_title attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}

- (void)addArrowNode
{
    ASImageNode *arrowNode = [[ASImageNode alloc]init];
    arrowNode.layerBacked = YES;
    arrowNode.backgroundColor = [UIColor clearColor];
    arrowNode.image = [UIImage imageNamed:@"xiala"];
    [self addSubnode:arrowNode];
    _arrowNode = arrowNode;
}

- (void)addLineNode
{
    ASImageNode *lineNode = [[ASImageNode alloc]init];
    lineNode.layerBacked = YES;
    lineNode.backgroundColor = RGB_255(230,230,230);
    [self addSubnode:lineNode];
    _lineNode = lineNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _arrowNode.style.preferredSize = CGSizeMake(9, 15);
    _lineNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, 0.5);
    
    ASStackLayoutSpec *titleLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[_titleNode,_arrowNode]];
    titleLayout.style.flexGrow = YES;
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(15, 10, 15, 10) child:_areaSecondLevel == YES ? _titleNode : titleLayout];
    
    ASStackLayoutSpec *lineLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[insetLayout,_lineNode]];
    
    return lineLayout;
}

@end
